Proper connect_port
[juce-lv2.git] / juce / source / extras / the jucer / src / model / components / jucer_SliderHandler.h
blobeeb022bdeda2a22d6f0c0e9787494e2bb7b51d9a
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_SLIDERHANDLER_JUCEHEADER__
27 #define __JUCER_SLIDERHANDLER_JUCEHEADER__
29 //==============================================================================
30 /**
32 class SliderHandler : public ComponentTypeHandler
34 public:
35 //==============================================================================
36 SliderHandler()
37 : ComponentTypeHandler ("Slider", "Slider", typeid (Slider), 150, 24)
39 registerColour (Slider::backgroundColourId, "background", "bkgcol");
40 registerColour (Slider::thumbColourId, "thumb", "thumbcol");
41 registerColour (Slider::trackColourId, "track", "trackcol");
42 registerColour (Slider::rotarySliderFillColourId, "rotary fill", "rotarysliderfill");
43 registerColour (Slider::rotarySliderOutlineColourId, "rotary outln", "rotaryslideroutline");
44 registerColour (Slider::textBoxTextColourId, "textbox text", "textboxtext");
45 registerColour (Slider::textBoxBackgroundColourId, "textbox bkgd", "textboxbkgd");
46 registerColour (Slider::textBoxHighlightColourId, "textbox highlt", "textboxhighlight");
47 registerColour (Slider::textBoxOutlineColourId, "textbox outln", "textboxoutline");
50 //==============================================================================
51 Component* createNewComponent (JucerDocument*)
53 return new Slider ("new slider");
56 //==============================================================================
57 XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
59 XmlElement* e = ComponentTypeHandler::createXmlFor (comp, layout);
61 Slider* const s = dynamic_cast <Slider*> (comp);
62 e->setAttribute ("min", s->getMinimum());
63 e->setAttribute ("max", s->getMaximum());
64 e->setAttribute ("int", s->getInterval());
65 e->setAttribute ("style", sliderStyleToString (s->getSliderStyle()));
66 e->setAttribute ("textBoxPos", textBoxPosToString (s->getTextBoxPosition()));
67 e->setAttribute ("textBoxEditable", s->isTextBoxEditable());
68 e->setAttribute ("textBoxWidth", s->getTextBoxWidth());
69 e->setAttribute ("textBoxHeight", s->getTextBoxHeight());
70 e->setAttribute ("skewFactor", s->getSkewFactor());
72 return e;
75 bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
77 if (! ComponentTypeHandler::restoreFromXml (xml, comp, layout))
78 return false;
80 Slider* const s = dynamic_cast <Slider*> (comp);
82 s->setRange (xml.getDoubleAttribute ("min", 0.0),
83 xml.getDoubleAttribute ("max", 10.0),
84 xml.getDoubleAttribute ("int", 0.0));
86 s->setSliderStyle (sliderStringToStyle (xml.getStringAttribute ("style", "LinearHorizontal")));
88 s->setTextBoxStyle (stringToTextBoxPos (xml.getStringAttribute ("textBoxPos", "TextBoxLeft")),
89 ! xml.getBoolAttribute ("textBoxEditable", true),
90 xml.getIntAttribute ("textBoxWidth", 80),
91 xml.getIntAttribute ("textBoxHeight", 20));
93 s->setSkewFactor (xml.getDoubleAttribute ("skewFactor", 1.0));
95 return true;
98 //==============================================================================
99 const String getCreationParameters (Component* component)
101 return quotedString (component->getName());
104 void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
106 ComponentTypeHandler::fillInCreationCode (code, component, memberVariableName);
108 Slider* const s = dynamic_cast <Slider*> (component);
110 String r;
111 r << memberVariableName << "->setRange ("
112 << s->getMinimum() << ", " << s->getMaximum() << ", " << s->getInterval()
113 << ");\n"
114 << memberVariableName << "->setSliderStyle (Slider::"
115 << sliderStyleToString (s->getSliderStyle()) << ");\n"
116 << memberVariableName << "->setTextBoxStyle (Slider::"
117 << textBoxPosToString (s->getTextBoxPosition())
118 << ", " << boolToString (! s->isTextBoxEditable())
119 << ", " << s->getTextBoxWidth() << ", " << s->getTextBoxHeight() << ");\n"
120 << getColourIntialisationCode (component, memberVariableName);
122 if (needsCallback (component))
123 r << memberVariableName << "->addListener (this);\n";
125 if (s->getSkewFactor() != 1.0)
126 r << memberVariableName << "->setSkewFactor (" << s->getSkewFactor() << ");\n";
128 r << '\n';
129 code.constructorCode += r;
132 void fillInGeneratedCode (Component* component, GeneratedCode& code)
134 ComponentTypeHandler::fillInGeneratedCode (component, code);
136 if (needsCallback (component))
138 String& callback = code.getCallbackCode ("public SliderListener",
139 "void",
140 "sliderValueChanged (Slider* sliderThatWasMoved)",
141 true);
143 if (callback.isNotEmpty())
144 callback << "else ";
146 const String memberVariableName (code.document->getComponentLayout()->getComponentMemberVariableName (component));
147 const String userCodeComment ("UserSliderCode_" + memberVariableName);
149 callback
150 << "if (sliderThatWasMoved == " << memberVariableName
151 << ")\n{\n //[" << userCodeComment << "] -- add your slider handling code here..\n //[/" << userCodeComment << "]\n}\n";
155 //==============================================================================
156 void getEditableProperties (Component* component, JucerDocument& document, Array <PropertyComponent*>& properties)
158 ComponentTypeHandler::getEditableProperties (component, document, properties);
160 Slider* s = dynamic_cast <Slider*> (component);
161 jassert (s != 0);
163 properties.add (new SliderRangeProperty (s, document, "minimum", 0));
164 properties.add (new SliderRangeProperty (s, document, "maximum", 1));
165 properties.add (new SliderRangeProperty (s, document, "interval", 2));
166 properties.add (new SliderTypeProperty (s, document));
167 properties.add (new SliderTextboxProperty (s, document));
168 properties.add (new SliderTextboxEditableProperty (s, document));
169 properties.add (new SliderTextboxSizeProperty (s, document, true));
170 properties.add (new SliderTextboxSizeProperty (s, document, false));
171 properties.add (new SliderSkewProperty (s, document));
173 addColourProperties (component, document, properties);
176 static bool needsCallback (Component* slider)
178 return true; //xxx should be a property
181 private:
182 //==============================================================================
183 class SliderTypeProperty : public ComponentChoiceProperty <Slider>
185 public:
186 SliderTypeProperty (Slider* slider, JucerDocument& document)
187 : ComponentChoiceProperty <Slider> ("type", slider, document)
189 choices.add ("Linear Horizontal");
190 choices.add ("Linear Vertical");
191 choices.add ("Linear Bar");
192 choices.add ("Rotary");
193 choices.add ("Rotary HorizontalDrag");
194 choices.add ("Rotary VerticalDrag");
195 choices.add ("Inc/Dec Buttons");
196 choices.add ("Two Value Horizontal");
197 choices.add ("Two Value Vertical");
198 choices.add ("Three Value Horizontal");
199 choices.add ("Three Value Vertical");
202 void setIndex (int newIndex)
204 const Slider::SliderStyle types[] = { Slider::LinearHorizontal,
205 Slider::LinearVertical,
206 Slider::LinearBar,
207 Slider::Rotary,
208 Slider::RotaryHorizontalDrag,
209 Slider::RotaryVerticalDrag,
210 Slider::IncDecButtons,
211 Slider::TwoValueHorizontal,
212 Slider::TwoValueVertical,
213 Slider::ThreeValueHorizontal,
214 Slider::ThreeValueVertical };
216 if (newIndex >= 0 && newIndex < numElementsInArray (types))
218 document.perform (new SliderTypeChangeAction (component, *document.getComponentLayout(), types [newIndex]),
219 "Change Slider style");
223 int getIndex() const
225 const Slider::SliderStyle types[] = { Slider::LinearHorizontal,
226 Slider::LinearVertical,
227 Slider::LinearBar,
228 Slider::Rotary,
229 Slider::RotaryHorizontalDrag,
230 Slider::RotaryVerticalDrag,
231 Slider::IncDecButtons,
232 Slider::TwoValueHorizontal,
233 Slider::TwoValueVertical,
234 Slider::ThreeValueHorizontal,
235 Slider::ThreeValueVertical };
237 for (int i = 0; i < numElementsInArray (types); ++i)
238 if (types [i] == dynamic_cast <Slider*> (component)->getSliderStyle())
239 return i;
241 return -1;
244 private:
245 class SliderTypeChangeAction : public ComponentUndoableAction <Slider>
247 public:
248 SliderTypeChangeAction (Slider* const comp, ComponentLayout& layout, const Slider::SliderStyle newState_)
249 : ComponentUndoableAction <Slider> (comp, layout),
250 newState (newState_)
252 oldState = comp->getSliderStyle();
255 bool perform()
257 showCorrectTab();
258 getComponent()->setSliderStyle (newState);
259 changed();
260 return true;
263 bool undo()
265 showCorrectTab();
266 getComponent()->setSliderStyle (oldState);
267 changed();
268 return true;
271 Slider::SliderStyle newState, oldState;
275 //==============================================================================
276 class SliderTextboxProperty : public ComponentChoiceProperty <Slider>
278 public:
279 SliderTextboxProperty (Slider* slider, JucerDocument& document)
280 : ComponentChoiceProperty <Slider> ("text position", slider, document)
282 choices.add ("No text box");
283 choices.add ("Text box on left");
284 choices.add ("Text box on right");
285 choices.add ("Text box above");
286 choices.add ("Text box below");
289 void setIndex (int newIndex)
291 const Slider::TextEntryBoxPosition types[] = { Slider::NoTextBox,
292 Slider::TextBoxLeft,
293 Slider::TextBoxRight,
294 Slider::TextBoxAbove,
295 Slider::TextBoxBelow };
297 if (newIndex >= 0 && newIndex < numElementsInArray (types))
299 document.perform (new SliderTextBoxChangeAction (component, *document.getComponentLayout(), types [newIndex]),
300 "Change Slider textbox");
304 int getIndex() const
306 const Slider::TextEntryBoxPosition types[] = { Slider::NoTextBox,
307 Slider::TextBoxLeft,
308 Slider::TextBoxRight,
309 Slider::TextBoxAbove,
310 Slider::TextBoxBelow };
312 for (int i = 0; i < numElementsInArray (types); ++i)
313 if (types [i] == component->getTextBoxPosition())
314 return i;
316 return -1;
319 private:
320 class SliderTextBoxChangeAction : public ComponentUndoableAction <Slider>
322 public:
323 SliderTextBoxChangeAction (Slider* const comp, ComponentLayout& layout, const Slider::TextEntryBoxPosition newState_)
324 : ComponentUndoableAction <Slider> (comp, layout),
325 newState (newState_)
327 oldState = comp->getTextBoxPosition();
330 bool perform()
332 showCorrectTab();
333 getComponent()->setTextBoxStyle (newState,
334 ! getComponent()->isTextBoxEditable(),
335 getComponent()->getTextBoxWidth(),
336 getComponent()->getTextBoxHeight());
337 changed();
338 return true;
341 bool undo()
343 showCorrectTab();
344 getComponent()->setTextBoxStyle (oldState,
345 ! getComponent()->isTextBoxEditable(),
346 getComponent()->getTextBoxWidth(),
347 getComponent()->getTextBoxHeight());
348 changed();
349 return true;
352 Slider::TextEntryBoxPosition newState, oldState;
356 //==============================================================================
357 class SliderTextboxEditableProperty : public ComponentBooleanProperty <Slider>
359 public:
360 SliderTextboxEditableProperty (Slider* slider, JucerDocument& document)
361 : ComponentBooleanProperty <Slider> ("text box mode", "Editable", "Editable", slider, document)
365 void setState (bool newState)
367 document.perform (new SliderEditableChangeAction (component, *document.getComponentLayout(), newState),
368 "Change Slider editability");
371 bool getState() const
373 return component->isTextBoxEditable();
376 private:
377 class SliderEditableChangeAction : public ComponentUndoableAction <Slider>
379 public:
380 SliderEditableChangeAction (Slider* const comp, ComponentLayout& layout, const bool newState_)
381 : ComponentUndoableAction <Slider> (comp, layout),
382 newState (newState_)
384 oldState = comp->isTextBoxEditable();
387 bool perform()
389 showCorrectTab();
390 getComponent()->setTextBoxIsEditable (newState);
391 changed();
392 return true;
395 bool undo()
397 showCorrectTab();
398 getComponent()->setTextBoxIsEditable (oldState);
399 changed();
400 return true;
403 bool newState, oldState;
407 //==============================================================================
408 class SliderTextboxSizeProperty : public ComponentTextProperty <Slider>
410 public:
411 SliderTextboxSizeProperty (Slider* slider, JucerDocument& document, const bool isWidth_)
412 : ComponentTextProperty <Slider> (isWidth_ ? "text box width" : "text box height",
413 12, false, slider, document),
414 isWidth (isWidth_)
418 void setText (const String& newText)
420 document.perform (new SliderBoxSizeChangeAction (component, *document.getComponentLayout(), isWidth, newText.getIntValue()),
421 "Change Slider textbox size");
424 const String getText() const
426 return String (isWidth ? component->getTextBoxWidth()
427 : component->getTextBoxHeight());
430 private:
431 const bool isWidth;
433 class SliderBoxSizeChangeAction : public ComponentUndoableAction <Slider>
435 public:
436 SliderBoxSizeChangeAction (Slider* const comp, ComponentLayout& layout, const bool isWidth_, int newSize_)
437 : ComponentUndoableAction <Slider> (comp, layout),
438 isWidth (isWidth_),
439 newSize (newSize_)
441 oldSize = isWidth ? comp->getTextBoxWidth()
442 : comp->getTextBoxHeight();
445 bool perform()
447 showCorrectTab();
449 if (isWidth)
450 getComponent()->setTextBoxStyle (getComponent()->getTextBoxPosition(),
451 ! getComponent()->isTextBoxEditable(),
452 newSize,
453 getComponent()->getTextBoxHeight());
454 else
455 getComponent()->setTextBoxStyle (getComponent()->getTextBoxPosition(),
456 ! getComponent()->isTextBoxEditable(),
457 getComponent()->getTextBoxWidth(),
458 newSize);
459 changed();
460 return true;
463 bool undo()
465 showCorrectTab();
467 if (isWidth)
468 getComponent()->setTextBoxStyle (getComponent()->getTextBoxPosition(),
469 ! getComponent()->isTextBoxEditable(),
470 oldSize,
471 getComponent()->getTextBoxHeight());
472 else
473 getComponent()->setTextBoxStyle (getComponent()->getTextBoxPosition(),
474 ! getComponent()->isTextBoxEditable(),
475 getComponent()->getTextBoxWidth(),
476 oldSize);
477 changed();
478 return true;
481 bool isWidth;
482 int newSize, oldSize;
486 //==============================================================================
487 class SliderRangeProperty : public ComponentTextProperty <Slider>
489 public:
490 SliderRangeProperty (Slider* slider, JucerDocument& document,
491 const String& name, const int rangeParam_)
492 : ComponentTextProperty <Slider> (name, 15, false, slider, document),
493 rangeParam (rangeParam_)
497 void setText (const String& newText)
499 double state [3];
500 state [0] = component->getMinimum();
501 state [1] = component->getMaximum();
502 state [2] = component->getInterval();
504 state [rangeParam] = newText.getDoubleValue();
506 document.perform (new SliderRangeChangeAction (component, *document.getComponentLayout(), state),
507 "Change Slider range");
510 const String getText() const
512 Slider* s = dynamic_cast <Slider*> (component);
513 jassert (s != 0);
515 switch (rangeParam)
517 case 0:
518 return String (s->getMinimum());
520 case 1:
521 return String (s->getMaximum());
523 case 2:
524 return String (s->getInterval());
526 default:
527 jassertfalse
528 break;
531 return String::empty;
534 private:
535 const int rangeParam;
537 class SliderRangeChangeAction : public ComponentUndoableAction <Slider>
539 public:
540 SliderRangeChangeAction (Slider* const comp, ComponentLayout& layout, const double newState_[3])
541 : ComponentUndoableAction <Slider> (comp, layout)
543 newState [0] = newState_ [0];
544 newState [1] = newState_ [1];
545 newState [2] = newState_ [2];
547 oldState [0] = comp->getMinimum();
548 oldState [1] = comp->getMaximum();
549 oldState [2] = comp->getInterval();
552 bool perform()
554 showCorrectTab();
555 getComponent()->setRange (newState[0], newState[1], newState[2]);
556 changed();
557 return true;
560 bool undo()
562 showCorrectTab();
563 getComponent()->setRange (oldState[0], oldState[1], oldState[2]);
564 changed();
565 return true;
568 double newState[3], oldState[3];
572 //==============================================================================
573 class SliderSkewProperty : public ComponentTextProperty <Slider>
575 public:
576 SliderSkewProperty (Slider* slider, JucerDocument& document)
577 : ComponentTextProperty <Slider> ("skew factor", 12, false, slider, document)
581 void setText (const String& newText)
583 const double skew = jlimit (0.001, 1000.0, newText.getDoubleValue());
585 document.perform (new SliderSkewChangeAction (component, *document.getComponentLayout(), skew),
586 "Change Slider skew");
589 const String getText() const
591 Slider* s = dynamic_cast <Slider*> (component);
592 jassert (s != 0);
594 return String (s->getSkewFactor());
597 private:
598 class SliderSkewChangeAction : public ComponentUndoableAction <Slider>
600 public:
601 SliderSkewChangeAction (Slider* const comp, ComponentLayout& layout, const double newValue_)
602 : ComponentUndoableAction <Slider> (comp, layout)
604 newValue = newValue_;
605 oldValue = comp->getSkewFactor();
608 bool perform()
610 showCorrectTab();
611 getComponent()->setSkewFactor (newValue);
612 changed();
613 return true;
616 bool undo()
618 showCorrectTab();
619 getComponent()->setSkewFactor (oldValue);
620 changed();
621 return true;
624 double newValue, oldValue;
628 //==============================================================================
629 static const String sliderStyleToString (Slider::SliderStyle style)
631 switch (style)
633 case Slider::LinearHorizontal:
634 return "LinearHorizontal";
635 case Slider::LinearVertical:
636 return "LinearVertical";
637 case Slider::LinearBar:
638 return "LinearBar";
639 case Slider::Rotary:
640 return "Rotary";
641 case Slider::RotaryHorizontalDrag:
642 return "RotaryHorizontalDrag";
643 case Slider::RotaryVerticalDrag:
644 return "RotaryVerticalDrag";
645 case Slider::IncDecButtons:
646 return "IncDecButtons";
647 case Slider::TwoValueHorizontal:
648 return "TwoValueHorizontal";
649 case Slider::TwoValueVertical:
650 return "TwoValueVertical";
651 case Slider::ThreeValueHorizontal:
652 return "ThreeValueHorizontal";
653 case Slider::ThreeValueVertical:
654 return "ThreeValueVertical";
656 default:
657 jassertfalse
658 break;
661 return String::empty;
664 static Slider::SliderStyle sliderStringToStyle (const String& s)
666 if (s == "LinearHorizontal")
667 return Slider::LinearHorizontal;
668 else if (s == "LinearVertical")
669 return Slider::LinearVertical;
670 else if (s == "LinearBar")
671 return Slider::LinearBar;
672 else if (s == "Rotary")
673 return Slider::Rotary;
674 else if (s == "RotaryHorizontalDrag")
675 return Slider::RotaryHorizontalDrag;
676 else if (s == "RotaryVerticalDrag")
677 return Slider::RotaryVerticalDrag;
678 else if (s == "IncDecButtons")
679 return Slider::IncDecButtons;
680 else if (s.startsWithIgnoreCase ("TwoValueHoriz"))
681 return Slider::TwoValueHorizontal;
682 else if (s.startsWithIgnoreCase ("TwoValueVert"))
683 return Slider::TwoValueVertical;
684 else if (s.startsWithIgnoreCase ("ThreeValueHoriz"))
685 return Slider::ThreeValueHorizontal;
686 else if (s.startsWithIgnoreCase ("ThreeValueVert"))
687 return Slider::ThreeValueVertical;
689 jassertfalse
690 return Slider::LinearHorizontal;
693 static const String textBoxPosToString (const Slider::TextEntryBoxPosition pos)
695 switch (pos)
697 case Slider::NoTextBox:
698 return "NoTextBox";
699 case Slider::TextBoxLeft:
700 return "TextBoxLeft";
701 case Slider::TextBoxRight:
702 return "TextBoxRight";
703 case Slider::TextBoxAbove:
704 return "TextBoxAbove";
705 case Slider::TextBoxBelow:
706 return "TextBoxBelow";
707 default:
708 jassertfalse
709 break;
712 return String::empty;
715 static const Slider::TextEntryBoxPosition stringToTextBoxPos (const String& s)
717 if (s == "NoTextBox")
718 return Slider::NoTextBox;
719 else if (s == "TextBoxLeft")
720 return Slider::TextBoxLeft;
721 else if (s == "TextBoxRight")
722 return Slider::TextBoxRight;
723 else if (s == "TextBoxAbove")
724 return Slider::TextBoxAbove;
725 else if (s == "TextBoxBelow")
726 return Slider::TextBoxBelow;
728 jassertfalse
729 return Slider::TextBoxLeft;
734 #endif // __JUCER_SLIDERHANDLER_JUCEHEADER__